package com.atguigu.dga.meta.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga.meta.bean.TableMetaInfoQuery;
import com.atguigu.dga.meta.bean.TableMetaInfoVO;
import com.atguigu.dga.meta.service.TableMetaInfoExtraService;
import com.atguigu.dga.meta.service.TableMetaInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据表 前端控制器
 * </p>
 *
 * @author tubage
 * @since 2024-03-15
 */
@RestController
@RequestMapping("/tableMetaInfo")
public class TableMetaInfoController {
    @Autowired
    TableMetaInfoService tableMetaInfoService;
    @Autowired
    TableMetaInfoExtraService tableMetaInfoExtraService;

    /**
     * 手动更新全库元数据
     * @param schemaName
     * @param assessDate
     * @return
     */
    @PostMapping("/init-tables/{schemaName}/{assessDate}")
    public String initTables(@PathVariable("schemaName") String schemaName,
                             @PathVariable("assessDate") String assessDate) {
        try {
            tableMetaInfoService.initTableMetaInfo(schemaName, assessDate);
        } catch (Exception e) {
            throw new RuntimeException("手动更新全部元数据失败");
        }
        return "success";
    }

    /**
     * 辅助信息保存接口
     * @param tableMetaInfoExtra
     * @return
     */
    @PostMapping("/tableExtra")
    public String tableExtra(@RequestBody TableMetaInfoExtra tableMetaInfoExtra) {
        // 修改时间
        tableMetaInfoExtra.setUpdateTime(new Date());
        tableMetaInfoExtraService.updateById(tableMetaInfoExtra);
        return "success";
    }

    /**
     * 单表详情
     * @param tableId
     * @return
     */
    @GetMapping("/table/{tableId}")
    public TableMetaInfo table(@PathVariable("tableId") Long tableId) {
        TableMetaInfo tableMetaInfo = tableMetaInfoService.getById(tableId);
        TableMetaInfoExtra tableMetaInfoExtra = tableMetaInfoExtraService.getOne(
                new QueryWrapper<TableMetaInfoExtra>()
                        .eq("schema_name",tableMetaInfo.getSchemaName())
                        .eq("table_name",tableMetaInfo.getTableName())
        );
        tableMetaInfo.setTableMetaInfoExtra(tableMetaInfoExtra);
        return tableMetaInfo;
    }


    /**
     * 表信息列表
     *
     * 客户端的请求: http://dga.gmall.com/tableMetaInfo/table-list
     *
     * 请求参数： pageNo     页码
     *          pageSize   每页显示的条数
     *          tableName  模糊查询的表
     *          schemaName 查询的库
     *          dwLevel    查询的层级
     */
    @GetMapping("/table-list")
    public String tableList(TableMetaInfoQuery tableMetaInfoQuery) {
        // 查询表信息列表
        List<TableMetaInfoVO> tableMetaInfoVOList =
                tableMetaInfoService.getTableMetaInfoVOListByConditionAndPage(tableMetaInfoQuery) ;

        // 查询结果总数
        Long total = tableMetaInfoService.getTableMetaInfoVOCountByCondition(tableMetaInfoQuery);

        // 封装返回结果JSON
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("total" , total  ) ;
        jsonObj.put("list" , tableMetaInfoVOList ) ;

        return jsonObj.toJSONString() ;
    }
}
